package com.sina.shop.foot;

import com.sina.shop.domain.foot.Foot;
import com.sina.shop.service.foot.FootService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-08-10:48
 */
@RestController
@RequestMapping("/foot")
public class FootController extends BaseController {

    @Autowired
    @Qualifier("footService")
    private FootService footService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getFootByAdminId", method = RequestMethod.GET)
    public List<Foot> getFootByAdminId(@RequestParam(value = "adminId") Long adminId) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getFootByAdminId, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return footService.getFootByAdminId(adminId);
    }

    @RequestMapping(value = "/deleteFoot", method = RequestMethod.POST)
    public Integer deleteFoot(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteFoot, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return footService.deleteFoot(id);
    }

    @RequestMapping(value = "/saveFoot", method = RequestMethod.POST)
    public Foot saveFoot(@RequestBody(required = false)Foot foot) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveFoot, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return footService.saveFoot(foot);
    }

    @RequestMapping(value = "/updateFoot", method = RequestMethod.POST)
    public Foot updateFoot(@RequestBody(required = false)Foot foot) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateFoot, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return footService.updateFoot(foot);
    }
}
